var init_images = function(url){
    img = {
        'del': [url+'/images/page_cross_bw.png',url+'/images/page_cross.png']
    };
};

var create_dom = function(html){
    var empty = new Element('div', {'html':html});
    var el = empty.getFirst();
    return el;
};

var form_send = function(el){
    el.addEvent('submit', function(e) {
        //Prevents the default submit event from loading a new page.
        e.stop();
        
        var b = this.getElement('input[type=submit]');
        var div = this.getParent();
        b.set('disabled', true)

        var req = new Request.JSON({ 
            url: el.action,
            onComplete: function(response) { 
                b.set('disabled', false);
            }, 
            onSuccess: function(response){ 
                if (response.success) {
                    var item = create_dom(response.html);
                    $('todos').getElement('ul').grab(item, "top");
                    item.highlight('#FF0');
                    el.reset();
                }
                else alert(response.message);
            } 
        }).post(el); 
    }); 
};

var completeTask = function (id, item){
    item.set('send', {url:'/finish/'+id,
        onSuccess:function(){
            var p = $('task_' + id);
            p.fade('out');
            p.dispose()
        }
    });
    item.send();
};

var deleteTask = function (id){
    if (confirm("Do you want to delete this todo #" + id + "?")){
        var req = new Request.JSON({ 
            url: '/delete/' + id,
            onSuccess: function(response){ 
                var p = $('task_' + id);
                p.fade('out');
                p.dispose()
            } 
        });
        req.send(); 
    }
};

var post_comment = function(el){
    el.addEvent('submit', function(e) {
        //Prevents the default submit event from loading a new page.
        e.stop();
        
        var b = this.getElement('input[type=submit]');
        b.set('disabled', true)

        var req = new Request.JSON({ 
            url: el.action,
            onComplete: function(response) { 
                b.set('disabled', false);
            }, 
            onSuccess: function(response){ 
                if (response.success) {
                    var target = $('task-comments');
                    target.set('html', target.get('html')+response.content);
                    target.getLast().highlight();
                    el.reset();
                }
                else alert(response.message);
            } 
        }).post(el); 
    }); 
};

var get_users = function(url){
    $('task-users').load(url); 
};

var get_comments = function(url){
    $('task-comments').load(url);
};

var deleteComment = function (url, id){
    var req = new Request.JSON({ 
        url: url,
        onSuccess: function(response){ 
            var p = $('comment_' + id);
            p.fade('out');
            p.dispose()
        } 
    });
    req.send(); 
};

var add_user = function(el){
    el.addEvent('submit', function(e) {
        //Prevents the default submit event from loading a new page.
        e.stop();
        
        var b = this.getElement('input[type=submit]');
        b.set('disabled', true)

        var req = new Request.JSON({ 
            url: el.action,
            onComplete: function(response) { 
                b.set('disabled', false);
            }, 
            onSuccess: function(response){ 
                if (response.success) {
                    var target = $('task-users');
                    target.set('html', target.get('html')+response.content);
                    target.getLast().highlight();
                }
                else alert(response.message);
            } 
        }).post(el); 
    }); 
};

var get_todo_files = function(url){
    $('file-list').load(url);
};

var delete_file = function (todo_id, file_id){
    if (confirm("Do you want to delete this file?")){
        var req = new Request.JSON({ 
            url: '/deletefile',
            onSuccess: function(response){ 
                var p = $('file_' + file_id);
                p.fade('out');
                p.dispose()
            } 
        });
        req.get({todo_id:todo_id, file_id:file_id}); 
    }
};
